import time
import yaml
import os
from api import client
from common.route import *
from common.read_yaml import get_yaml_filepath
import os
import configparser
from common.read_config import test_token
data = get_yaml_filepath(Environ='data.yaml')  # 读取config下的data数据


class Login_token(client.HttpClient):

    def __init__(self,test_info,token):
        super().__init__()
        self.username = test_info["user_name"]
        # print("rrrrr:",self.username)
        self.phoneValid = test_info["phoneValid"]
        result = self.login(username=self.username,phoneValid=self.phoneValid)
        # print("3333333:",result)
        # assert result == 200,"前台登录失败"
        self.token = result['body']['accessToken']  # 获取token
        # 把token值写入配置文件中
        self.conf_file = os.path.abspath(os.path.dirname(os.path.dirname(__file__)) + r"\configs\token.ini")  # 报存文件的路径
        config = configparser.ConfigParser()
        # 打开配置文件
        config.read(self.conf_file,encoding='utf-8')
        # 在配置文件中设置token的值
        config.set("token","{}".format(token),str(self.token))
        config.write(open(self.conf_file,'w'))  # w ：修改配置文件的内容并保存

        # 将token写入yaml文件
        # data1 = r'D:\接口自动化\悦管家_接口自动化框架\configs\test\data.yaml'
        # # dic_token = {'{}'.format(token):self.token}
        # data['{}'.format(token)] = self.token
        # # print('4444444444',dic_token)
        # with open(data1,encoding='utf-8',mode='w') as f:
        #     yaml.dump(data,stream=f,allow_unicode=True)

    def login(self,username,phoneValid):
        etc = {}
        url = login_data['url']
        method = login_data['method']
        body = {"zip":"+86","username":username,"phoneValid":phoneValid,"pushRegister":"","source":"","monitor_name":"","openId":"","fromApp":False}
        url = self.get_full_url(url,etc=etc)
        return self.send(url,method=method,body=body)

def login_test(test_info,test_token):
    return Login_token(test_info,test_token)
def login_test2(test_info,test2_token):
    return Login_token(test_info,test2_token)






